package com.lemons.hackaton.app;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import com.google.zxing.NotFoundException;
import com.lemons.hackaton.algorithm.Scheduler;
import com.lemons.hackaton.app.data.DocxReader;
import com.lemons.hackaton.app.data.QRDecoder;
import com.lemons.hackaton.app.data.ReadPdfFiles;
import com.lemons.hackaton.app.data.ReadXML;
import com.lemons.hackaton.obj.Journey;

public class Application {
	private static String dataLocation="";
	public static Journey journey; 
	public static Scheduler scheduler;
	
	static JFrame guiFrame;	
	
	static JTextField fieldDataFolder = new JTextField("D://lemon//data//");
	static JTextField fieldOutFolder = new JTextField("D://lemon//out//");
	private static String outFolder = "";
	
	public static void main(String[] args)  {
		
		drawComponents();
		
	}
	
	private static void drawComponents() {


		guiFrame = new JFrame("Journey profits");
		if(guiFrame==null) return;
		guiFrame.setSize(600, 400);
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.getContentPane().setLayout(new GridBagLayout());
		guiFrame.getContentPane().setMinimumSize(guiFrame.getMaximumSize());
		GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5,0,5,0), 0, 0);
		
		JLabel labelDataFolder = new JLabel("Data Folder");
				
		guiFrame.getContentPane().add(labelDataFolder, c);
		
		c = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5,0,5,0), 0, 0);

		
		
		fieldDataFolder.setSize(new Dimension(300, 20));
		fieldDataFolder.setMaximumSize(new Dimension(300, 20));
		fieldDataFolder.setMaximumSize(new Dimension(300, 20));
        
        
		guiFrame.getContentPane().add(fieldDataFolder, c);
		
		c = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5,0,5,0), 0, 0);

		JLabel labelOutFolder = new JLabel("Out Folder");
		guiFrame.getContentPane().add(labelOutFolder, c);
		
		c =new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5,0,5,0), 0, 0);


		fieldOutFolder.setSize(new Dimension(300, 20));
		fieldOutFolder.setMaximumSize(new Dimension(300, 20));
		fieldOutFolder.setMaximumSize(new Dimension(300, 20));
		guiFrame.getContentPane().add(fieldOutFolder , c);
		
		JButton button = new JButton("Maximize Profits");
		c = new GridBagConstraints(0, 10, 2, 1, 0, 0 , GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0);
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				startProcessing();				
			}
		});   
		
		guiFrame.getContentPane().add(button, c);
		guiFrame.setVisible(true);
	}

	public static void startProcessing(){
		if(validateInput()){
        //@TODO init journey and add data to it
		System.out.println("START: " + System.currentTimeMillis());
		try{
		journey = ReadXML.readXML(dataLocation);
		QRDecoder qrDecoder = new QRDecoder();
		qrDecoder.processQRImages(dataLocation + "\\additional-files\\");

        
		DocxReader docReader = new DocxReader();
		docReader.processDocFiles(dataLocation + "\\additional-files\\");
		
        ReadPdfFiles.processPDFS(dataLocation + "\\additional-files\\");
        
//        System.out.println(journey.toString());
        
        //@TODO process data
		
        
        scheduler = new Scheduler(journey);
		scheduler.schedule();

		//scheduler.writeScheduleToEXCELFile();
		//@TODO OUTPUT data
//		} catch (FileNotFoundException ex){
//			ex.printStackTrace();
//		} catch (NotFoundException ex){
//			ex.printStackTrace();
//		} catch (IOException ex){
//			ex.printStackTrace();
//		} catch (InvalidFormatException ex){
//			ex.printStackTrace();
//		}
		}catch(Exception e){
			e.printStackTrace();
		}
		
        System.out.println("END: " + System.currentTimeMillis());
		}
	}

	private static boolean validateInput() {
		boolean rez = false;
        try{  
		  File f= new File(fieldDataFolder.getText());
		  if(!f.isDirectory()){
			  JOptionPane.showMessageDialog(fieldDataFolder, "Nu este director");

		  } else {
			  dataLocation = fieldDataFolder.getText();
              rez = true;
		  }
        } catch(Exception ex){
        	JOptionPane.showMessageDialog(fieldDataFolder, "Nu este director");

        }
		
        
        try{  
		  File f= new File(fieldOutFolder.getText());
		  if(!f.isDirectory()){
			  JOptionPane.showMessageDialog(fieldOutFolder, "Nu este director");
		  } else {
			  outFolder = fieldDataFolder.getText();
			  rez = true;
		  }
        } catch(Exception ex){
        	JOptionPane.showMessageDialog(fieldOutFolder, "Nu este director");
        }
        return rez;
	}

}
